package com.fand.site.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.common.service.CommonService;
import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.site.dto.DomainNaDto;
import com.fand.site.dto.SiteDto;
import com.fand.site.validator.DomainNaValidator;
import com.fand.site.validator.SiteValidator;
import com.fand.site.vo.SiteVo;
import com.google.common.collect.Maps;

/**
 * 站
 * 
 * @author yaoHT
 */
@Controller
public class SiteController {
	/**
	 * 公共服务
	 */
	@Qualifier(CommonService.BEAN_ID)
	@Autowired(required = false)
	private CommonService commonService;
	
	@Qualifier(SiteValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator siteValidator;
	
	@Qualifier(DomainNaValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator domainNaValidator;
	
	@RequestMapping(value = { "/admin/site/domainNa/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminSiteDomainNaDelete(final String ids) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getSiteService().deleteDomainNaByIds(ids);
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/edit.htm" }, method = RequestMethod.GET)
	public String adminSiteDomainNaEdit(final String id, final Model model) {
		final DomainNaDto domainNa = this.commonService.getSiteService().selectDomainNaBy1ForMappedObject(id);
		
		model.addAttribute("domainNa", domainNa);
		
		final List<SiteDto> siteList = this.commonService.getSiteService().selectSiteBy1ForMappedObjectList();
		
		model.addAttribute("siteList", siteList);
		
		return "admin/site/domainNa/edit";
	}
	
	@RequestMapping(value = { "/admin/site/domainNa.htm", "/admin/site/domainNa/index.htm" })
	public String adminSiteDomainNaIndex(final SiteVo siteVo, final Model model) {
		model.addAttribute("domainNa", siteVo.getDomainNa());
		
		final List<SiteDto> siteList = this.commonService.getSiteService().selectSiteBy1ForMappedObjectList();
		
		model.addAttribute("siteList", siteList);
		
		return "admin/site/domainNa/index";
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminSiteDomainNaIndexPagination(final SiteVo siteVo) {
		final PaginationDto<Map<String, Object>> domainNaPagination = this.commonService.getSiteService()
		        .paginationDomainNaBy1ForMapList(siteVo.getDomainNa());
		
		final Map<String, Object> domainNaPaginationMap = Maps.newHashMap();
		
		domainNaPaginationMap.put("total", domainNaPagination.getTotalSize());
		domainNaPaginationMap.put("rows", domainNaPagination.getDataList());
		
		return domainNaPaginationMap;
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/new.htm" }, method = RequestMethod.GET)
	public String adminSiteDomainNaNew(final SiteVo siteVo, final Model model) {
		final List<SiteDto> siteList = this.commonService.getSiteService().selectSiteBy1ForMappedObjectList();
		
		model.addAttribute("siteList", siteList);
		
		return "admin/site/domainNa/new";
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminSiteDomainNaSave(final SiteVo siteVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.siteValidator.validate(siteVo.getDomainNa(), bindingResult);
			
			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;
				
				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}
				
				throw new Exception(message);
			}
			
			this.commonService.getSiteService().saveDomainNaBy1ForObject(siteVo.getDomainNa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/validationByCode.json" })
	public @ResponseBody
	Boolean adminSiteDomainNaValidationByCode(final SiteVo siteVo) {
		try {
			final int count = this.commonService.getSiteService().selectDomainNaByIdAndCodeForObject(
			        siteVo.getDomainNa().getId(), siteVo.getDomainNa().getCode());
			
			return (count < 1);
		} catch (final Exception e) {
		}
		
		return false;
	}
	
	@RequestMapping(value = { "/admin/site/domainNa/view.htm" }, method = RequestMethod.GET)
	public String adminSiteDomainNaView(final String id, final Model model) {
		final DomainNaDto domainNa = this.commonService.getSiteService().selectDomainNaBy1ForMappedObject(id);
		
		model.addAttribute("domainNa", domainNa);
		
		return "admin/site/domainNa/view";
	}
	
	@RequestMapping(value = { "/admin/site/site/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminSiteSiteDelete(final String ids) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getSiteService().deleteSiteByIds(ids);
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	@RequestMapping(value = { "/admin/site/site/edit.htm" }, method = RequestMethod.GET)
	public String adminSiteSiteEdit(final String id, final Model model) {
		final SiteDto site = this.commonService.getSiteService().selectSiteBy1ForMappedObject(id);
		
		model.addAttribute("site", site);
		
		return "admin/site/site/edit";
	}
	
	@RequestMapping(value = { "/admin/site/site.htm", "/admin/site/site/index.htm" })
	public String adminSiteSiteIndex(final SiteVo siteVo) {
		return "admin/site/site/index";
	}
	
	@RequestMapping(value = { "/admin/site/site/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminSiteSiteIndexPagination(final SiteVo siteVo) {
		final PaginationDto<Map<String, Object>> sitePagination = this.commonService.getSiteService()
		        .paginationSiteBy1ForMapList(siteVo.getSite());
		
		final Map<String, Object> sitePaginationMap = Maps.newHashMap();
		
		sitePaginationMap.put("total", sitePagination.getTotalSize());
		sitePaginationMap.put("rows", sitePagination.getDataList());
		
		return sitePaginationMap;
	}
	
	@RequestMapping(value = { "/admin/site/site/new.htm" }, method = RequestMethod.GET)
	public String adminSiteSiteNew(final SiteVo siteVo) {
		return "admin/site/site/new";
	}
	
	@RequestMapping(value = { "/admin/site/site/reload.json" })
	public @ResponseBody
	Map<String, Object> adminSiteSiteReload() {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getSiteService().reload();
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	@RequestMapping(value = { "/admin/site/site/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminSiteSiteSave(final SiteVo siteVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.siteValidator.validate(siteVo.getSite(), bindingResult);
			
			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;
				
				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}
				
				throw new Exception(message);
			}
			
			this.commonService.getSiteService().saveSiteBy1ForObject(siteVo.getSite());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}
	
	@RequestMapping(value = { "/admin/site/site/validationByCode.json" })
	public @ResponseBody
	Boolean adminSiteSiteValidationByCode(final SiteVo siteVo) {
		try {
			final int count = this.commonService.getSiteService().selectSiteByIdAndCodeForObject(
			        siteVo.getSite().getId(), siteVo.getSite().getCode());
			
			return (count < 1);
		} catch (final Exception e) {
		}
		
		return false;
	}
	
	@RequestMapping(value = { "/admin/site/site/view" }, method = RequestMethod.GET)
	public String adminSiteSiteView(final String id, final Model model) {
		final SiteDto site = this.commonService.getSiteService().selectSiteBy1ForMappedObject(id);
		
		model.addAttribute("site", site);
		
		return "admin/site/site/view";
	}
	
	@RequestMapping(value = { "/site/site.htm", "/site/site/index.htm" }, method = RequestMethod.GET)
	public String siteSiteindex() {
		return "site/site/index";
	}
}
